﻿var db = null; //new Map();

//function device_storage_by_port(port, datas) {
//    if (db == null)
//        db = new Map();
//    var dc = db.get(port);

//    if (dc == null) {
//        dc = datas;
//        db.set(port, dc);
//        return;
//    }
//    for (var i = 0; i < datas.length; i++) {
//        let deviceid = datas[i].deviceid;
//        dc[deviceid] = datas[i];
//    }
//}

//不同的port对应不同的应用,8051端口 8052端口
function device_storage(port, deviceid, content) {
    if (db == null)
        db = new Map();
    var dc = db.get(port);

    if (dc == null) {
        //console.log("new map child");
        //console.log(port,deviceid,content);
        dc = {};
        dc[deviceid] = content;
        db.set(port, dc);
    }
    else {
        dc[deviceid] = content;
    }
   // console.log(dc);
}


function device_get_bydevice(port, deviceid) {
    if (db == null)
        return null;
    var temp = db.get(port);
    if (temp == null)
        return null;
    //console.log('get',port,deviceid);
   //console.log("temp1",temp[deviceid]);
    return temp[deviceid];
}

function device_get_by_port(port) {
    if (db == null)
        return null;
    var temp = db.get(port);
    if (temp == null)
        return null;
    return temp;
}
function device_del_bydevice(port, deviceid) {
    if (db == null)
        return null;
    var temp = db.get(port);
    if (temp == null)
        return null;
    delete temp[deviceid];
    //db.delete(deviceid);
}

//data:     必须要保存到数据库的数据,
//data_ext: 只需要保存到即时数据里的数据
//data_callback 保存到数据库的回调函数
function tcpcallback(port ,data, data_ext, data_callback) {
    //查询缓存中是否有数据
      //console.log('data',data);
    // if (data.deviceid == null) {
    //     console.log("error,tcpcallback() deviceid is null");
    //     return;
    // }
    if(data == null && data_ext == null)
        return;
    var deviceid = null;
    var obj = null;
    if (data != null) {
        deviceid = data.deviceid;
        obj = device_get_bydevice(port, deviceid);
    //console.log("obj",obj);
        if (obj == null)
            obj = {};
        for (var key in data) {
            obj[key] = data[key];
        }
    }
    if (data_ext != null){
        deviceid = data_ext.deviceid;
        obj = device_get_bydevice(port,deviceid);
        if(obj == null)
            obj ={};
        for (var key in data_ext) {
            obj[key] = data_ext[key];
        }
    }

    //存储到内存
    device_storage(port, deviceid, obj);
 
    //存储到数据库
    if (data_callback != null && data != null)
        data_callback(port, data);
  
}
//这个接口是新增的,为通用接口,但子协议现在还不通用
function tcpcallback_1(port, data, data_callback) {
    var time = null;
    var deviceid = null;
    if (data.headfield != null && data.headfield.time!=null)
        time = data.headfield.time;
    if (data.deviceid != null)
        deviceid = data.deviceid

    if (data.field != null) {
        data.field.time = time;//时间
        data.field.deviceid = deviceid; //
        //保存
    }
    if (data.protocol != null) {
        //包含子协议
        var writeobjs = [];
        if (Array.prototype.isPrototypeOf(data.protocol) == true) {//如果是数组
            for (var i = 0; i < data.protocol.length; i++) {
                var detail = data.protocol[i];
                for (var j = 0; j < detail.value.length; j++) {
                    var writeobj = {};
                    writeobj.deviceid = deviceid;
                    writeobj.name = "sensor:" + (i+1);
                    //for(var )
                    if (detail.head.point_num != null) {//小数位数
                        writeobj.point_num = detail.head.point_num;
                    }
                    if (detail.head.dm != null) {
                        writeobj.dm = detail.head.dm;//正负
                    }
                    writeobj.value = detail.value[i];
                    writeobjs.push(writeobj);
                }
                //写入数据库
            }
            if (data_callback != null) {
                data_callback(writeobjs);
            }
        }
    }

}
function get_port_device_data(port, deviceids) {
    if (Array.prototype.isPrototypeOf(deviceids) != true) { //如果不是数组
        console.log("is not an array");
        return null;
    }
     var obj = [];
    for(var i =0;i< deviceids.length;i++){
        var o = device_get_bydevice(port, deviceids[i]);
        if(o!=null)
            obj.push(o);
    }

    return obj;
}

function get_port_device_data_1(port, deviceid) {
    //var obj = [];
    //for (var i = 0; i < deviceids.length; i++) {
    return device_get_bydevice(port, deviceid);
}


//tcpcallback(8051, { deviceid:1001,temp: 1, press: 2 }, null, null);

//var obj = get_port_device_data(8051, [1001]);
//console.log(obj);

exports.tcpcallback = tcpcallback;
exports.get_port_device_data = get_port_device_data;
exports.get_port_device_data_1 = get_port_device_data_1;



//exports.device_storage = device_storage;
//exports.device_get_bydevice = device_get_bydevice;
//exports.device_get_bycompany = device_get_bycompany;
//exports.device_storage_bycompany = device_storage_bycompany;
//exports.device_del_bydevice = device_del_bydevice;

//cache 测试代码
/*
var obj1 = {temp:"1",press:"2",time:"2017",deviceid:"01010101"};
var obj2 = { temp: "1", press: "2", time: "2017", deviceid: "01010102" };
var obj3 = { temp: "1", press: "2", time: "2017", deviceid: "01010103" };
device_storage("test1", "01010101", obj1);
device_storage("test2", "01010102", obj2);
device_storage("test1", "01010103", obj3);

//console.log(device_get_bydevice("test2", "01010102"));
var temp = device_get_bycompany("test1");
for (var prop in temp) {
    console.log(temp[prop]);
}
var obj = JSON.stringify(temp);
console.log(obj);
*/


//buffer 测试代码
//console.log(config.get('redis'));

//var buf = new Buffer([0x69, 0x00, 0x01, 0x00, 0x01, 0x03, 0x00, 0x31, 0x00, 0x01, 0xb7, 0x84, 0x16]);
//var buf2 = new Buffer([0x69, 0x00, 0x01, 0x00, 0x01, 0x04, 0x08, 0x46, 0x7A, 0x0B, 0xE8, 0x41, 0xF1, 0x00, 0x00, 0xB2, 0x87, 0x16]);

//var v_temp = buf2.readFloatBE(7);
//var v_press = buf2.readFloatBE(11);

//var v_deviceid = buf.toString('hex', 1, 5);

//const buffer = Buffer.allocUnsafe(16);
//buffer[0] = 0x01;
//buffer[1] = 0x02;


//buf2.copy(buf2, 0, 5, 18);
//console.log(buf2.toString('hex'));
//console.log(v_press);